#include "changepassword.h"
#include "ui_changepassword.h"
#include <QGraphicsDropShadowEffect>
#include <QNetworkRequest>
#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>
#include "messagepops.h"
#include <QTimer>
#include "operationdocument.h"
#include "function/function.h"

extern QByteArray token;//http返回id码


//修改密码界面
ChangePassword::ChangePassword(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ChangePassword)
{
    ui->setupUi(this);

    this->setWindowModality(Qt::ApplicationModal); //阻塞除当前窗体之外的所有的窗体
    this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
    this->setWindowFlags(Qt::FramelessWindowHint);   //设置无边框窗口

    //设置具体阴影
    QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setOffset(0, 0);

    //阴影颜色
    shadow_effect->setColor(QColor(38, 78, 119, 127));
    //阴影半径
    shadow_effect->setBlurRadius(10);
    ui->label->setGraphicsEffect(shadow_effect);

    ui->label_6->hide();//隐藏密码不一致提示   
    setpush();
    ui->pushButton->setEnabled(false);//当输入框没有内容时禁止确定按钮
    ui->lineEdit->setEchoMode(QLineEdit::Password);//隐藏密码
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);//隐藏密码
    ui->lineEdit_3->setEchoMode(QLineEdit::Password);//隐藏密码

    OperationDocument::setqss(ui->lineEdit,":/new/prefix1/resource/myStyleSheet2.qss");
    OperationDocument::setqss(ui->lineEdit_2,":/new/prefix1/resource/myStyleSheet2.qss");
    OperationDocument::setqss(ui->lineEdit_3,":/new/prefix1/resource/myStyleSheet2.qss");

    QFont font;
//    font.setFamily(QStringLiteral("Microsoft YaHei"));//设置字体
//    font.setPixelSize(20);//字体大小
    font.setLetterSpacing(QFont::PercentageSpacing,120);//120字间距，默认100
    ui->label_2->setFont(font);
    ui->label_3->setFont(font);
    ui->label_4->setFont(font);
    ui->label_5->setFont(font);
    ui->label_6->setFont(font);
    ui->pushButton->setFont(font);


    //初始化定时器对象
    deletetime=new QTimer(this);
    //设置定时时间
    deletetime->setInterval(100);  //0.1秒
    //关联timeout()信号与自定义的槽函数
    connect(deletetime,SIGNAL(timeout()),this,SLOT(deletetimer()));

    mps=new MessagePops();//初始化提示窗口对象
    mps->hide();
    connect(mps,SIGNAL(sendMessage(QString)),this,SLOT(receMessage(QString)));

    mps2=new MessagePops();//初始化提示窗口对象
    mps2->hide();
    connect(mps2,SIGNAL(sendMessage(QString)),this,SLOT(receMessage2(QString)));

    //关联http信号与槽
    connect(&naManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));


}

ChangePassword::~ChangePassword()
{
    delete ui;
}

//更改密码成功点击确定
void ChangePassword::receMessage(QString data)
{
    if(data == "1")
    {
        //关闭当前界面--》主界面
        on_pushButton_5_clicked();
    }
}

//更改密码失败点击确定
void ChangePassword::receMessage2(QString data)
{
    if(data == "1")
    {

    }
}

void ChangePassword::deletetimer()
{
    deletetime->stop();//关闭定时器

    delete this;
}

void ChangePassword::requestFinished(QNetworkReply *reply)
{
    QByteArray buf = reply->readAll();
    qDebug()<<__LINE__<<buf;

    //把获取的json数据标准化存储
    QJsonParseError myerr;
    QJsonDocument mydoc=QJsonDocument::fromJson(buf,&myerr);

    //获取最外面的对象
    QJsonObject obj=mydoc.object();

    QString message=obj.value("message").toString();
    qDebug()<<__LINE__<<"message="<<message;

    if(message == "更改成功")
    {
        mps->setText("更改密码成功");
        mps->show();

    }
    else
    {
        mps2->setText("更改密码失败");
        mps2->show();
    }
}

//点击了确定按钮
void ChangePassword::on_pushButton_clicked()
{
    oldpassword = ui->lineEdit->text();
    newpassword1 = ui->lineEdit_2->text();
    newpassword2 = ui->lineEdit_3->text();

    if(newpassword1 != newpassword2)
    {
        ui->label_6->show();
        return;
    }

    QNetworkRequest request;

    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
    request.setUrl(QUrl::fromUserInput(URLName_2("update-password/")));

    QJsonObject object;
    object.insert("oldpassword",oldpassword);
    object.insert("newpassword",newpassword1);

    QByteArray byte_array = QJsonDocument(object).toJson();
    qDebug()<<__LINE__<< "user  info " << object<<"\n"<<token;
    request.setRawHeader("token",token);
    naManager.post(request,byte_array);//发起post请求



}

//隐藏显示旧密码
void ChangePassword::on_pushButton_2_clicked()
{
    //设置密码隐藏
    if(Password_conceal)
    {
        Password_conceal=false;
        ui->lineEdit->setEchoMode(QLineEdit::Password);//隐藏密码
        ui->pushButton_2->setStyleSheet("border-image: url(:/new/prefix1/resource/15.png);");
    }
    else
    {
        Password_conceal=true;
        ui->lineEdit->setEchoMode(QLineEdit::Normal);//显示密码
        ui->pushButton_2->setStyleSheet("border-image: url(:/new/prefix1/resource/16.png);");
    }
}

//隐藏显示新密码
void ChangePassword::on_pushButton_3_clicked()
{
    //设置密码隐藏
    if(Password_conceal2)
    {
        Password_conceal2=false;
        ui->lineEdit_2->setEchoMode(QLineEdit::Password);//隐藏密码
        ui->pushButton_3->setStyleSheet("border-image: url(:/new/prefix1/resource/15.png);");
    }
    else
    {
        Password_conceal2=true;
        ui->lineEdit_2->setEchoMode(QLineEdit::Normal);//显示密码
        ui->pushButton_3->setStyleSheet("border-image: url(:/new/prefix1/resource/16.png);");
    }
}

//隐藏显示确定新密码
void ChangePassword::on_pushButton_4_clicked()
{
    //设置密码隐藏
    if(Password_conceal3)
    {
        Password_conceal3=false;
        ui->lineEdit_3->setEchoMode(QLineEdit::Password);//隐藏密码
        ui->pushButton_4->setStyleSheet("border-image: url(:/new/prefix1/resource/15.png);");
    }
    else
    {
        Password_conceal3=true;
        ui->lineEdit_3->setEchoMode(QLineEdit::Normal);//显示密码
        ui->pushButton_4->setStyleSheet("border-image: url(:/new/prefix1/resource/16.png);");
    }
}

//点击关闭按钮
void ChangePassword::on_pushButton_5_clicked()
{
    deletetime->start();
    emit sendChangePassword("1");
}

//旧密码输入框按下回车时触发
void ChangePassword::on_lineEdit_returnPressed()
{
    //光标移到新密码输入框
    ui->lineEdit_2->setFocus();
}

void ChangePassword::on_lineEdit_2_returnPressed()
{
    ui->lineEdit_3->setFocus();
}

void ChangePassword::on_lineEdit_3_returnPressed()
{
    
}

//旧密码输入框中的文本改变时，发出此信号
void ChangePassword::on_lineEdit_textChanged(const QString &arg1)
{
    if(arg1.isEmpty())
    {
        OperationDocument::setqss(ui->lineEdit,":/new/prefix1/resource/myStyleSheet2.qss");
    }
    else
    {
        OperationDocument::setqss(ui->lineEdit,":/new/prefix1/resource/myStyleSheet.qss");
    }

    setpush();

}

void ChangePassword::on_lineEdit_2_textChanged(const QString &arg1)
{
    if(arg1.isEmpty())
    {
        OperationDocument::setqss(ui->lineEdit_2,":/new/prefix1/resource/myStyleSheet2.qss");
    }
    else
    {
        OperationDocument::setqss(ui->lineEdit_2,":/new/prefix1/resource/myStyleSheet.qss");
    }

    //隐藏密码不一致提示
    ui->label_6->hide();

    setpush();
}

void ChangePassword::on_lineEdit_3_textChanged(const QString &arg1)
{
    if(arg1.isEmpty())
    {
        OperationDocument::setqss(ui->lineEdit_3,":/new/prefix1/resource/myStyleSheet2.qss");
    }
    else
    {
        OperationDocument::setqss(ui->lineEdit_3,":/new/prefix1/resource/myStyleSheet.qss");
    }

    //隐藏密码不一致提示
    ui->label_6->hide();

    setpush();
}

void ChangePassword::setpush()
{
    if(ui->lineEdit->text().isEmpty() || ui->lineEdit_2->text().isEmpty() || ui->lineEdit_3->text().isEmpty())
    {
        //当输入框没有内容时禁止登入按钮
        ui->pushButton->setEnabled(false);
        ui->pushButton->setStyleSheet("background: #CCCCCC;border-bottom-left-radius :16px;border-bottom-right-radius :16px;font-size: 18px;font-family: Microsoft YaHei;color: #FBFBFB;");
    }
    else
    {
        ui->pushButton->setEnabled(true);
        ui->pushButton->setStyleSheet("background: #1B89CC;border-bottom-left-radius :16px;border-bottom-right-radius :16px;font-size: 18px;font-family: Microsoft YaHei;color: #FBFBFB;");
    }
}
